package com.shop.cereshop.agent.controller.search;

import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.domain.agentSearch.CereAgentSearchHistory;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.agent.page.product.ProductVO;
import com.shop.cereshop.agent.param.product.ProductDTO;
import com.shop.cereshop.agent.param.search.SearchDTO;
import com.shop.cereshop.agent.service.product.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 搜索接口
 * @date: 2025/10/18 15:18
 */
@RequestMapping("search")
@RestController
@Api(value = "搜索接口", tags = "搜索接口")
public class SearchController {
  @Autowired
  private ProductService productService;

  /**
   * @description: 搜索历史查询接口
   * @param:
   * @return:
   * @date: 2025/10/20 11:07
   */
  @GetMapping("/history")
  @ApiOperation(value = "搜索历史查询接口")
  public Result<Page<CereAgentSearchHistory>> searchHistory(HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Long agentUserId = user.getAgentUserId();
    SearchDTO searchDTO = new SearchDTO();
    searchDTO.setAgentId(agentUserId);
    Page<CereAgentSearchHistory> searchHistoryList = productService.getSearchHistoryList(searchDTO);
    return Result.success(searchHistoryList);
  }

  /**
   * @description: 批量删除搜索历史接口
   * @param:
   * @return:
   * @date: 2025/10/20 11:17
   */
  @PostMapping("/delete/history")
  @ApiOperation(value = "批量删除搜索历史接口")
  public Result<Object> deleteSearchHistory(@RequestBody List<Long> searchHistoryIds, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    Long agentUserId = user.getAgentUserId();
    SearchDTO searchDTO = new SearchDTO();
    searchDTO.setAgentId(agentUserId);
    searchDTO.setHistoryId(searchHistoryIds);
    boolean flag = productService.deleteSearchHistory(searchDTO);
    return flag ? Result.success() : Result.failed(CoReturnFormat.FAIL);
  }
}
